﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;


namespace ThePresenter.IO
{
	public class Xaml : AbstractFormat, IWriter, IReader
	{
		#region IFormat Members
		/// <summary>
		/// Gets the filter that is shown in the <see cref="Microsoft.Win32.OpenFileDialog"/> and <see cref="Microsoft.Win32.SaveFileDialog"/> dialogs. If is null, this format does not support writing or reading to / from file. Can be null.
		/// </summary>
		/// <value>The filter shown in the dialogs.</value>
		public override string Filter
		{
			get
			{
				return "Native XAML format (*.xml)|*.xml";
			}
		}
		#endregion


		#region IWriter Members
		/// <summary>
		/// Writes the specified presentation to the stream.
		/// </summary>
		/// <param name="config">The configuration object.</param>
		/// <param name="presentation">The presentation.</param>
		/// <param name="stream">The stream.</param>
		/// <exception cref="ThePresenter.WriteException"/>
		public void Write(DependencyObject config, Presentation presentation, Stream stream)
		{
			if (presentation == null)
				throw new ArgumentNullException("presentation");

			if (stream == null)
				throw new ArgumentNullException("stream");

			XamlWriter.Save(presentation, stream);
		}
		#endregion


		#region IReader Members
		/// <summary>
		/// Reads the specified stream and tries to create new presentation from it.
		/// </summary>
		/// <param name="config">The configuration object.</param>
		/// <param name="stream">The stream to read from.</param>
		/// <param name="presentation">The presentation.</param>
		/// <exception cref="ThePresenter.ReadException"/>
		public void Read(DependencyObject config, Stream stream, out Presentation presentation)
		{
			if (stream == null)
				throw new ArgumentNullException("stream");


			presentation = XamlReader.Load(stream) as Presentation;
		}
		#endregion
	}
}
